package com.fzhucc.modules.sendDataToQz.util;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Base64;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class SendDataGeneralUtil {

    /**
     * 字典查询
     *
     * @throws Exception
     */
    public String selectDictByDictType(String baseUrl, String sysid,String dict) throws Exception {
        //请求方法路径
        String url = "/general/selectDictByDictType";
        //设置对接系统ID
        RequestUtil.sysid = sysid;
        //请求体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dictType", dict);
        //请求头
        Map<String, String> headers = new LinkedHashMap<>();
        return RequestUtil.doPost(baseUrl + url, headers, jsonObject.toJSONString());
    }

    /**
     * 所属工会查询
     *
     * @throws Exception
     */
    public String selectDeptByDeptName(String baseUrl, String sysid, String deptName) throws Exception {
        //请求方法路径
        String url = "/general/selectDeptByDeptName";
        //设置对接系统ID
        RequestUtil.sysid = sysid;
        //请求体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deptName", deptName);
        jsonObject.put("pageNum", 1);
        jsonObject.put("pageSize", 10);

        //请求头
        Map<String, String> headers = new LinkedHashMap<>();
        return RequestUtil.doPost(baseUrl + url, headers, jsonObject.toJSONString());
    }

    /**
     * 所在地区查询aaas
     *
     * @throws Exception
     */
    public String selectAreaByAreaName(String baseUrl, String sysid, String name) throws Exception {
        //请求方法路径
        String url = "/general/selectAreaByAreaName";
        //设置对接系统ID
        RequestUtil.sysid = sysid;
        //请求体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
//        jsonObject.put("pageNum", 1);
        jsonObject.put("pageSize", 10);
        //请求头
        Map<String, String> headers = new LinkedHashMap<>();
        return RequestUtil.doPost(baseUrl + url, headers, jsonObject.toJSONString());
    }

    /**
     * 附件上传
     *
     * @throws Exception
     */
    public String attachmentUpload(String baseUrl, String sysid, String picName, String path) throws Exception {
        //请求方法路径
        String url = "/general/attachmentUpload";
        //设置对接系统ID
        RequestUtil.sysid = sysid;
        //请求体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", picName);
        jsonObject.put("content", generateBase64(path));
        //System.out.println(jsonObject.toJSONString());
        //请求头
        Map<String, String> headers = new LinkedHashMap<>();
        return RequestUtil.doPost(baseUrl + url, headers, jsonObject.toJSONString());
    }


    /**
     * 文件编码为base64
     *
     * @param path 文件路径
     * @return
     * @throws Exception
     */
    public static String generateBase64(String path) throws IOException {
        if (path == null || path.isEmpty()) {
            throw new RuntimeException("文件不能为空！");
        }
        byte[] imageBytes;
        String base64EncoderImg = "";
        try {
            //TODO 因为是网络地址转成uri在转成File类型
            //URI uri = new URI(path);
            //File file = new File(uri);

            File file = new File(path);
            try (FileInputStream fis = new FileInputStream(path)) {
                imageBytes = new byte[(int) file.length()];
                fis.read(imageBytes);
            }
            /**
             * Java中在使用BASE64Enconder().encode()会出现字符串换行问题，这是因为RFC 822中规定，
             *   每72个字符中加一个换行符号，这样会造成在使用base64字符串时出现问题，
             *   所以我们在使用时要先用replaceAll("[\\s*\t\n\r]", "")解决换行的问题。
             */
            base64EncoderImg = Base64.getEncoder().encodeToString(imageBytes);
            base64EncoderImg = base64EncoderImg.replaceAll("[\\s*\t\n\r]", "");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return base64EncoderImg;
    }
}
